package fr.cephb.joperon.core.iterator;

import com.sleepycat.db.Cursor;
import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.db.DatabaseException;

/** 
 * An iterator iterating in a range of <b>KEYS</b> {@link com.sleepycat.db.DatabaseEntry}
 */
@Deprecated
public class BetweenIterator extends KeyRangeIterator
	{
	private byte endBytes[];
	private boolean includeEnd;
	/**
	 * @param cursor the cursor
	 * @param begin the start key 
	 * @param end the end key
	 * @param includeEnd a boolean specifiying if the end key should be included
	 */
	public BetweenIterator(
			Cursor cursor,
			DatabaseEntry begin,
			DatabaseEntry end,
			boolean includeEnd
			)
		{
		super(cursor, begin);
		this.endBytes=new byte[end.getSize()];
		this.includeEnd=includeEnd;
		System.arraycopy(end.getData(), 0, this.endBytes, 0, this.endBytes.length);
		}
	
	/* (non-Javadoc)
	 * @see fr.cephb.joperon.core.iterator.KeyRangeIterator#isInRange(com.sleepycat.db.DatabaseEntry)
	 */
	@Override
	protected boolean isInRange(DatabaseEntry entry) throws DatabaseException {
		byte data[]=entry.getData();
		int len= Math.min(endBytes.length, entry.getSize());
		int cmp=0;
		//System.err.println("End Compare \""+new String(endBytes)+"\""); 
		//System.err.println("Curr Compare \""+new String(entry.getData())+"\""); 
		for(int i=0;i< len;++i)
			{
			//System.err.println("Comparing "+(char) endBytes[i]+" "+ (char)data[i]);
			cmp= endBytes[i]-data[i] ;
			if( cmp > 0) return true;//data byte lower than end byte
			if( cmp < 0) return false;
			}
		cmp =  endBytes.length - entry.getSize() ;
		if( cmp > 0) return true;//end plus grand que current
		if( cmp < 0) return false;//end plus petit que current
		
		/* no difference: same size and same bytes.
		 * return TRUE if we include the end
		 * return FALSE if we do not include the end
		 */
		return includeEnd;
		}

}
